% To calculate the rating change time intervals

clc
clear all

conn = database('BDC','','');
%labels = {1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,-1};
labels = {1.5,2,2.5,3,3.5,4,4.5,-1}; %change to 7 ratings_2012Feb3
year_list=1997:2009;
DURATION = 1;
NUMBER_OF_YEAR = length(year_list) - DURATION;
conditions = {'maxPPDOS_sum >= 50000','maxPPDOS_sum >= 50000 and maxPPDOS_sum < 250000',...
    'maxPPDOS_sum >= 250000 and maxPPDOS_sum <1000000','maxPPDOS_sum >= 1000000'};
numRate = length(labels);
numGrp = length(conditions);
SG = cellstr(char('Sample 1: [$50k,+)','Sample A: [$50k, 250k)','Sample B: [$250k, 1000k)','Sample C: [$1000k, +)'));
startYear = input('Please enter a value for startYear:');
endYear = input('Please enter a value for endYear:');

Date1 = strcat(num2str(startYear),'-12-01');
Date2 = strcat(num2str(endYear),'-12-01');
disp( strcat(['Date Range: ' Date1 ' to ' Date2]));
interTime(numGrp,1) = struct('totalsVec',[],'totalsMat',[],'totalsRec',[]);


for cdIdx = 2:numGrp
    condition = char(conditions(cdIdx));
    sqlquery1 = strcat(['select Customer, modiFisDate, modiRate FROM final_v5_tbl where ' condition ' and modiFisDate BETWEEN ''' Date1 ''' and ''' Date2 ''' order by customer, modiFisDate']);%change to v5 table due to 7 ratings reset
    %sqlquery1 = strcat(['select Customer, modiFisDate, modiRate FROM final_v4_tbl where modiFisDate BETWEEN ''' Date1 ''' and ''' Date2 ''' and customer <=500 order by customer, modiFisDate']);
    
    results = fetch(conn, sqlquery1);
    disp(size(results));
    [interTime(cdIdx)] = interExaminTime(results,'labels',labels);
    figure
    hist(interTime(cdIdx).totalsRec,(endYear-startYear)*2);
    title(SG(cdIdx));
    xlabel('Jumping Time');
    ylabel('Frequency') 
%     x= str2num(get(gca,'YTickLabel'));
%     x=x./max(x);
%     set(gca,'YTickLabel',num2str(x));
end

% to count frequency% for time<1 =1 and >1..but not good bin shreshold
interTimeGrp = zeros(numGrp,3);  
interTimePcg = zeros(numGrp,3);
for cdIdx = 2:numGrp

    for i = 1: length(interTime(cdIdx).totalsRec)
        if interTime(cdIdx).totalsRec(i) < 1
        interTimeGrp(cdIdx,1) = interTimeGrp(cdIdx,1)+1;
        else if interTime(cdIdx).totalsRec(i) == 1
                interTimeGrp(cdIdx,2) = interTimeGrp(cdIdx,2)+1;
            else interTimeGrp(cdIdx,3) = interTimeGrp(cdIdx,3)+1;
            end
        end
    end
    
    for j = 1: 3
    interTimePcg(cdIdx,j)= interTimeGrp(cdIdx,j)/length(interTime(cdIdx).totalsRec);
    end
end

%% 
% to generate histgram for latex figure
% group 2 - 4 for year 1998-2005 and year 2005-2009
clc
clear all
load ('D:\My Documents\CIRANO\db_mySQL\interTimeExamine_grp2_4_yr97_04_MR7.mat','interTime') 
interTime1 = interTime;
load ('D:\My Documents\CIRANO\db_mySQL\interTimeExamine_grp2_4_yr04_09_MR7.mat','interTime') 
interTime2 = interTime;

% graphing into 2 graphs

numberOfBins1 = 0:0.25:7;
normalizedCountsGrp1 = zeros(3,length(numberOfBins1));
figure
for cdIdx = 2:4  
    subplot(3,1,(cdIdx-1));    
    [counts1, binValues1] = hist(interTime1(cdIdx).totalsRec,numberOfBins1);
    normalizedCounts1 = 100 * counts1 / sum(counts1);
    normalizedCountsGrp1 (cdIdx-1,:) = normalizedCounts1;
    bar(binValues1, normalizedCounts1,'barwidth', 1);
    set(gca,'XLim',[0 7]);
    set(gca,'XTick',numberOfBins1);
    set(gca,'YLim',[0 20]);
    title( strcat(SG(cdIdx),' for Year 97-05'));
    xlabel('Jumping Time');
    ylabel('Frequency [%]') 
    grid on
end

numberOfBins2 = 0:0.25:5;
normalizedCountsGrp2 = zeros(3,length(numberOfBins2));
figure
for cdIdx = 2:4  
    subplot(3,1,(cdIdx-1));    
    [counts2, binValues2] = hist(interTime2(cdIdx).totalsRec,numberOfBins2);
    normalizedCounts2 = 100 * counts2 / sum(counts2);
    normalizedCountsGrp2 (cdIdx-1,:) = normalizedCounts2;
    bar(binValues2, normalizedCounts2, 'barwidth', 1);
    set(gca,'XLim',[0 5]);
    set(gca,'XTick',numberOfBins2);
    set(gca,'YLim',[0 20]);
    title( strcat(SG(cdIdx),' for Year 05-09'));
    xlabel('Jumping Time');
    ylabel('Frequency [%]') 
    grid on
end


% graph two year group 97-04 and 05-09 into one graph
% for cdIdx = 2:4  
%     subplot(3,2,2*(cdIdx-1)-1);
%     x1 = 0:0.25:7;
%     hist(interTime1(cdIdx).totalsRec,x1);
%     set(gca,'XLim',[0 7])
%     set(gca,'XTick',x1)
%     title( strcat(SG(cdIdx),' for Year 97-05'));
%     xlabel('Jumping Time');
%     ylabel('Frequency') 
% end
% for cdIdx = 2:4  
%     subplot(3,2,2*(cdIdx-1));
%     x2 = 0:0.25:5;
%     hist(interTime2(cdIdx).totalsRec,x2);
%     set(gca,'XLim',[0 5])
%     title(strcat(SG(cdIdx),' for Year 05-09'));
%     xlabel('Jumping Time');
%     ylabel('Frequency') 
% end

